package org.van.financial.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.van.financial.model.BillDomain;
import org.van.financial.service.bill.BillService;
import org.van.financial.utils.APIResponse;
import org.van.financial.utils.GsonUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/api")
public class BillController extends BaseController{

    @Autowired
    private BillService billService;

    @PostMapping(value = "/bill")
    @ResponseBody
    public String getBillList(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(name = "bill_name" , required = true) String bill_name) {
       try {
            List<BillDomain> bills = billService.getBillByCond(bill_name);
            return GsonUtils.toJsonString(bills);
        } catch (Exception e){
           return "Error Get Bills";
       }
    }

    @PostMapping(value = "/addbill")
    @ResponseBody
    public APIResponse<?> addBill(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(name = "bill_name" , required = true) String bill_name,
//            @RequestParam(name = "bill_type" , required = true) String bill_type,
            @RequestParam(name = "bill_time" , required = true) String bill_time,
            @RequestParam(name = "bill_amount" , required = true) Integer bill_amount,
            @RequestParam(name = "bill_mode" , required = false) String bill_mode,
            @RequestParam(name = "bill_notes" , required = false) String bill_notes){
        BillDomain bill = new BillDomain();
        bill.setBill_name(bill_name);
//        bill.setBill_type(bill_type);
        bill.setBill_time(bill_time);
        bill.setBill_amount(bill_amount);
        if (null != bill_mode) bill.setBill_mode(bill_mode);
        if (null != bill_notes) bill.setBill_notes(bill_notes);
        try {
            billService.addNewBill(bill);
        } catch (Exception e){
            return APIResponse.fail("");
        }
        return APIResponse.success();
    }

    @PostMapping(value = "/deletebill")
    @ResponseBody
    public APIResponse<?> deleteBill(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(name = "uuid" , required = true) int uuid){
        try {
            billService.deleteBillById(uuid);
        } catch (Exception e){
            return APIResponse.fail("");
        }
        return APIResponse.success();
    }
}
